/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-14 22:08:03
 * @LastEditTime: 2022-03-27 22:56:32
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../../Console/H_NES_Console.h"
#include "../../ConstString/H_NES_ConstString.h"




extern void H_NES_Mapper_AccessException(H_NES_Console* Console,int RetVal,const char* Format,Huint16 ErrorAddress);
extern void H_NES_Mapper_Warning(H_NES_Console* Console,int RetVal,const char* Format,Huint16 ErrorAddress);

extern void H_NES_Mapper_DefaultGetProgress(void* v,void* Progress);
extern void H_NES_Mapper_DefaultLoadProgress(void* v,void* Progress);
extern int H_NES_Mapper_DefaultProgressSize(void* v);




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper4


static void MapperUpdate(void* v){
  H_NES_Console* Console;
  Hbyte* p;
  Hbyte Reg_8000;
  Hbyte* t_bytep;
  Huint32 ROM_Size;
  Huint32 Offset;

  Console=v;

  Reg_8000=Console->ThisMapper.Reg_8000;


  p=Console->Cartridge.Mapper.PRG_ROM;
  ROM_Size=Console->Cartridge.Mapper.PRG_ROM_Size;
  if((Reg_8000&0x40U)==0U){
    Offset=((Huint32)Console->ThisMapper.R[6])<<13;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[0]=t_bytep;

    Offset=((Huint32)Console->ThisMapper.R[7])<<13;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[1]=t_bytep;

    Offset=ROM_Size-0x2000*2;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[2]=t_bytep;
  }else{
    Offset=ROM_Size-0x2000*2;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[0]=t_bytep;

    Offset=((Huint32)Console->ThisMapper.R[7])<<13;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[1]=t_bytep;
    
    Offset=((Huint32)Console->ThisMapper.R[6])<<13;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[2]=t_bytep;
  }

  p=Console->Cartridge.Mapper.CHR_ROM;
  ROM_Size=Console->Cartridge.Mapper.CHR_ROM_Size;
  if(ROM_Size==0U){
    ROM_Size=0x2000U;
  }
  if((Reg_8000&0x80U)==0U){

    Offset=((Huint32)Console->ThisMapper.R[0]&0xFEU)<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[0]=&t_bytep[0];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[1]=&t_bytep[0x0400];

    Offset=((Huint32)Console->ThisMapper.R[1]&0xFEU)<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[2]=&t_bytep[0];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[3]=&t_bytep[0x0400];

    Offset=((Huint32)Console->ThisMapper.R[2])<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[4]=&t_bytep[0];

    Offset=((Huint32)Console->ThisMapper.R[3])<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[5]=&t_bytep[0];

    Offset=((Huint32)Console->ThisMapper.R[4])<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[6]=&t_bytep[0];

    Offset=((Huint32)Console->ThisMapper.R[5])<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[7]=&t_bytep[0];
  }else{
    Offset=((Huint32)Console->ThisMapper.R[2])<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[0]=&t_bytep[0];

    Offset=((Huint32)Console->ThisMapper.R[3])<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[1]=&t_bytep[0];

    Offset=((Huint32)Console->ThisMapper.R[4])<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[2]=&t_bytep[0];
    
    Offset=((Huint32)Console->ThisMapper.R[5])<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[3]=&t_bytep[0];

    Offset=((Huint32)Console->ThisMapper.R[0]&0xFEU)<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[4]=&t_bytep[0];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[5]=&t_bytep[0x0400];

    Offset=((Huint32)Console->ThisMapper.R[1]&0xFEU)<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[6]=&t_bytep[0];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[7]=&t_bytep[0x0400];
  }

}




static void PRG_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;
  Hbyte t_byte;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_AccessException(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGWriteAccess,Address);
      for(;;){
      }
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGRAMWriteAccess,Address);
      // for(;;){
      // }
    }

    p[Address-0x6000U]=Value;
  }else{
    switch(Address&0x6001U){
      case 0x0000U:
        Console->ThisMapper.Reg_8000=Value;
        MapperUpdate(Console);
        break;
      case 0x0001U:
        t_byte=Console->ThisMapper.Reg_8000&0x07U;
        if(t_byte<0x02U){
          Value&=(~0x01U);
        }
        Console->ThisMapper.R[t_byte]=Value;
        MapperUpdate(Console);
        break;
      case 0x2000U:
        if(Console->PPU.VRAM_ScreenType!=0x03U){
          if((Value&0x01U)==0U){
            //命名表水平排列
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=0x02U;
          }else{
            //命名表垂直排列
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=0x01U;
          }
        }
        break;
      case 0x2001U:
        //不实现
        break;
      case 0x4000U:
        Console->ThisMapper.Reg_C000=Value;
        break;
      case 0x4001U:
        Console->ThisMapper.IRQ_Timer_IsReload=0xFFU;
        break;
      case 0x6000U:
        Console->ThisMapper.IRQ_Enable=0x00U;
        Console->CPU.Status&=(~vH_NES_CPU_Status_IRQ_ExtIRQ_0);
        break;
      case 0x6001U:
        Console->ThisMapper.IRQ_Enable=0xFFU;
        break;
      default:
        break;
    }
  }
}
static Hbyte PRG_Read(Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_AccessException(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGReadAccess,Address);
      for(;;){
      }
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGRAMReadAccess,Address);
      // for(;;){
      // }
      return Address>>8;
    }

    return p[Address-0x6000U];
  }else{
    p=Console->ThisMapper.PRG_Banks.Base8000_8KB[(Address>>13)&0x0003U];
    
    return p[Address&0x1FFFU];
  }
}
static Huint16 PRG_Read16(Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_AccessException(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGReadAccess,Address);
      for(;;){
      }
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGRAMReadAccess,Address);
      // for(;;){
      // }
      return Address>>8;
    }

    p=&p[Address-0x6000U];

    return (Huint16)p[0]|((Huint16)p[1]<<8);
  }else{
    p=Console->ThisMapper.PRG_Banks.Base8000_8KB[(Address>>13)&0x0003U];

    p=&p[Address&0x1FFFU];
    
    return (Huint16)p[0]|((Huint16)p[1]<<8);
  }
}




static void CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;
  
  H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionCHRWriteAccess,Address);
}
static void CHR_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  Console->ThisMapper.CHR_Banks.Base0000_1KB[(Address>>10)&0x0007U][Address&0x03FFU]=Value;
}
static Hbyte CHR_Read(Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  return Console->ThisMapper.CHR_Banks.Base0000_1KB[(Address>>10)&0x0007U][Address&0x03FFU];
}




static void DeInit(void* v){
  H_NES_Console* Console;

  Console=v;
  
  if(Console->Cartridge.Mapper.PRG_RAM!=NULL){
    Console->Memory.Free(Console->Cartridge.Mapper.PRG_RAM);
  }
}

static void GetProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultGetProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  p[0]=Console->ThisMapper.Reg_8000;
  p[1]=Console->ThisMapper.Reg_A001;
  p[2]=Console->ThisMapper.Reg_C000;

  p[3]=Console->ThisMapper.IRQ_Timer_Cnt;
  p[4]=Console->ThisMapper.IRQ_Timer_IsReload;
  p[5]=Console->ThisMapper.IRQ_Enable;
  for(i=0;i<8;i++){
    p[6+i]=Console->ThisMapper.R[i];
  }

}

static void LoadProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultLoadProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  Console->ThisMapper.Reg_8000=p[0];
  Console->ThisMapper.Reg_A001=p[1];
  Console->ThisMapper.Reg_C000=p[2];

  Console->ThisMapper.IRQ_Timer_Cnt=p[3];
  Console->ThisMapper.IRQ_Timer_IsReload=p[4];
  Console->ThisMapper.IRQ_Enable=p[5];
  for(i=0;i<8;i++){
    Console->ThisMapper.R[i]=p[6+i];
  }
  
  MapperUpdate(Console);
}

static void MMC3_A12_Rise(unsigned int line,void* v){
  H_NES_Console* Console;
  Hbyte IRQ_Timer_Cnt;
  Hbyte Reg_C000;

  Console=v;


  IRQ_Timer_Cnt=Console->ThisMapper.IRQ_Timer_Cnt;

  //revA
  // if(Console->ThisMapper.IRQ_Timer_IsReload!=0x00U){
  //   Console->ThisMapper.IRQ_Timer_IsReload=0x00U;

  //   Reg_C000=Console->ThisMapper.Reg_C000;
  //   if((IRQ_Timer_Cnt==0x01U)&&(Reg_C000==0x00U)){
  //     if(Console->ThisMapper.IRQ_Enable!=0x00U){
  //       Console->CPU.Status|=vH_NES_CPU_Status_IRQ_ExtIRQ_0;
  //     }
  //   }
  //   Console->ThisMapper.IRQ_Timer_Cnt=Reg_C000;
  // }else{
  //   if(IRQ_Timer_Cnt>0x00U){
  //     IRQ_Timer_Cnt-=0x01U;
  //     Console->ThisMapper.IRQ_Timer_Cnt=IRQ_Timer_Cnt;

  //     if(IRQ_Timer_Cnt==0x00U){
  //       if(Console->ThisMapper.IRQ_Enable!=0x00U){
  //         Console->CPU.Status|=vH_NES_CPU_Status_IRQ_ExtIRQ_0;
  //       }
  //     }
  //   }else{
  //     Console->ThisMapper.IRQ_Timer_Cnt=Console->ThisMapper.Reg_C000;
  //   }
  // }
  
  //revB
  if(Console->ThisMapper.IRQ_Timer_IsReload!=0x00U){
    Console->ThisMapper.IRQ_Timer_IsReload=0x00U;

    Reg_C000=Console->ThisMapper.Reg_C000;
    IRQ_Timer_Cnt=Reg_C000;
  }else{
    if(IRQ_Timer_Cnt>0x00U){
      IRQ_Timer_Cnt-=0x01U;
    }else{
      IRQ_Timer_Cnt=Console->ThisMapper.Reg_C000;
    }
  }
  Console->ThisMapper.IRQ_Timer_Cnt=IRQ_Timer_Cnt;
  if(IRQ_Timer_Cnt==0x00U){
    if(Console->ThisMapper.IRQ_Enable!=0x00U){
      Console->CPU.Status|=vH_NES_CPU_Status_IRQ_ExtIRQ_0;
    }
  }

}

int H_NES_Mapper4_Init(void* v){
  H_NES_Console* Console;
  int i;

  Console=v;


  if(Console->Cartridge.Mapper.PRG_RAM_Size<0){
    Console->Cartridge.Mapper.PRG_RAM_Size=0x2000;
    Console->Cartridge.Mapper.PRG_RAM=Console->Memory.Malloc(0x2000);
    if(Console->Cartridge.Mapper.PRG_RAM==NULL){
      return vH_NES_Result_PRG_RAM_AllocFail;
    }
  }


  Console->ThisMapper.Reg_8000=0x00U;
  Console->ThisMapper.Reg_A001=0x00U;
  Console->ThisMapper.Reg_C000=0x00U;

  Console->ThisMapper.IRQ_Timer_Cnt=0x00U;
  Console->ThisMapper.IRQ_Timer_IsReload=0x00U;
  Console->ThisMapper.IRQ_Enable=0x00U;

  for(i=0;i<8;i++){
    Console->ThisMapper.R[i]=0x00U;
  }

  Console->ThisMapper.PRG_Banks.Base8000_8KB[3]=&Console->Cartridge.Mapper.PRG_ROM[Console->Cartridge.Mapper.PRG_ROM_Size-0x2000U];

  MapperUpdate(Console);


  Console->Cartridge.Mapper.DeInit=DeInit;

  
  Console->Cartridge.PRG_Write=PRG_Write;
  Console->Cartridge.PRG_Read=PRG_Read;
  Console->Cartridge.PRG_Read16=PRG_Read16;
  
  if(Console->Cartridge.Mapper.CHR_ROM_Size==0U){
    Console->Cartridge.CHR_Write=CHR_Write;
  }else{
    Console->Cartridge.CHR_Write=CHR_Exception_Write;
  }
  Console->Cartridge.CHR_Read=CHR_Read;

  Console->Cartridge.Progress.Size=14+H_NES_Mapper_DefaultProgressSize(Console);
  Console->Cartridge.Progress.Get=GetProgress;
  Console->Cartridge.Progress.Load=LoadProgress;

  Console->Cartridge.Option.MMC3_A12_Rise=MMC3_A12_Rise;
  
  

  return vH_NES_Result_OK;
}




#undef ThisMapper



